package com.rss;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListaFeeds extends ListActivity {

        private static final int ACTIVITY_BORRAR = 1;
        private static final int ACTIVITY_INSERTAR = 2;
        private static final int ACTIVITY_VER = 3;
        private static final int ACTIVITY_SUGGEST = 4;

        private RssDB db;
        private List<Feed> feeds;
        private Map<Long, Long> m;

        @Override
        protected void onCreate(Bundle icicle) {
                try {
                        super.onCreate(icicle);
                        Window w = getWindow();
                        w.requestFeature(Window.FEATURE_LEFT_ICON);
                        setContentView(R.layout.lista_feeds);
                        w.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
                                        R.drawable.icon);
                        db = new RssDB(this);
                        // db.open();
                        rellenaDatos();
                } catch (Throwable e) {
                        Log.e("RSSREADER listafeeds oncreate", e.toString());
                }
        }

        public boolean onCreateOptionsMenu(Menu menu) {
                super.onCreateOptionsMenu(menu);
        
                menu.add(0, ACTIVITY_SUGGEST, 0, R.string.menu_suggest);
                menu.add(0, ACTIVITY_INSERTAR, 1, R.string.menu_insert);
                menu.add(0, ACTIVITY_BORRAR, 2, R.string.menu_delete);
                
                return true;
        }

        public boolean onMenuItemSelected(int featureId, MenuItem item) {
                super.onMenuItemSelected(featureId, item);
                switch (item.getItemId()) {
                case ACTIVITY_INSERTAR:
                        crearFeed();
                        break;
                case ACTIVITY_BORRAR:

                        // TODO : revisar getSelection()
                        int pos = getSelectedItemPosition();
                        if (pos >= 0) {

                                db.open();
                                db.borrarFeed(feeds.get(getSelectedItemPosition()).feedId);
                                db.close();
                                rellenaDatos();
                        }
                        break;
                case ACTIVITY_SUGGEST:
                        suggest();
                        break;
                }

                return true;
        }

        private void crearFeed() {
                Intent i = new Intent(this, editorURL.class);
                startActivityForResult(i, ACTIVITY_INSERTAR);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                rellenaDatos();
        }

        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
                super.onListItemClick(l, v, position, id);

                Intent i = new Intent(this, ListaArticulo.class);
                i.putExtra("feed_id", feeds.get(position).feedId);
                i.putExtra("titulo", feeds.get(position).titulo);
                i.putExtra("url", feeds.get(position).url.toString());
                startActivityForResult(i, ACTIVITY_VER);
        }

        private void rellenaDatos() {

                List<String> items = new ArrayList<String>();
                db.open();
                feeds = db.obtenerFeeds();
                m = db.articulosSinleer();
                String cadenaAAnadir;
                db.close();
                Long cantidad = new Long(0);
                
                for (Feed feed : feeds) {
                        cantidad = m.get(feed.feedId);
                        String cantAux = new String();
                        if(cantidad == null){
                               cantAux = "NA";
                        }
                        else{
                        		cantAux = cantidad.toString();
                        }                         	
                        cadenaAAnadir = feed.titulo + " (" + cantAux  + ")";
                        items.add(cadenaAAnadir);
                }

                ArrayAdapter<String> notes = new ArrayAdapter<String>(this,
                                R.layout.fila_feeds, items);
                setListAdapter(notes);
        }
        
        public void suggest(){
                Intent i = new Intent(this, ListaSugerencias.class);
                startActivity(i);
        }

}
